<template>
  <div>
    <div class="m-3 p-3 pt-5 bg-white">
      <b-query :query-params="model.queryParam" :fields="fields" @query="queryPage" @reset="resetQueryParams" />
    </div>
    <div class="m-3 p-3 bg-white">
      <vxe-toolbar ref="xToolbar" custom :refresh="{ queryMethod: queryPage }">
        <template #buttons>
          <a-space>
            <a-button type="primary" pre-icon="ant-design:plus-outlined" @click="add">新建</a-button>
            <a-button preIcon="ant-design:sync-outlined" @click="importData">导入</a-button>
          </a-space>
        </template>
      </vxe-toolbar>
      <vxe-table
        row-id="id"
        ref="xTable"
        :data="pagination.records"
        :loading="loading"
        :sort-config="{ remote: true, trigger: 'cell' }"
        @sort-change="sortChange"
      >
        <vxe-column type="seq" title="序号" width="60" />
        <vxe-column field="specialityName" title="专业名称" :min-width="100" />
        <vxe-column field="specialityCode" title="专业代码" :min-width="100" />
        <vxe-column field="planTime" title="招生年份" :min-width="100" />
        <vxe-column field="passingScore" title="分数线" :min-width="100" />
        <vxe-column field="recruitNum" title="计划名额" :min-width="100" />
        <vxe-column field="num" title="剩余名额" :min-width="100" />
        <vxe-column fixed="right" width="100" :showOverflow="false" title="操作">
          <template #default="{ row }">
            <span>
              <a-link @click="edit(row)">编辑</a-link>
            </span>
            <a-divider type="vertical" />
            <span>
              <a-link danger @click="remove(row)">删除</a-link>
            </span>
          </template>
        </vxe-column>
      </vxe-table>
      <vxe-pager
        size="medium"
        :loading="loading"
        :current-page="pagination.current"
        :page-size="pagination.size"
        :total="pagination.total"
        @page-change="handleTableChange"
      />
      <major-edit ref="majorEdit" @ok="queryPage" />

      <basic-import-modal
        ref="basicImportModal"
        @ok="queryPage"
        template-url="/specialityPlan/getTemplate"
        upload-url="/specialityPlan/importExecl"
        template-name="专业计划.xlsx"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { computed, onMounted } from 'vue'
  import { $ref } from 'vue/macros'
  import { page, del } from './Major.api'
  import useTablePage from '/@/hooks/bootx/useTablePage'
  import MajorEdit from './MajorEdit.vue'
  import BQuery from '/@/components/Bootx/Query/BQuery.vue'
  import { useMessage } from '/@/hooks/web/useMessage'
  import { QueryField, STRING } from '/@/components/Bootx/Query/Query'
  import { VxeTableInstance, VxeToolbarInstance, VxePager, VxeTable, VxeToolbar } from 'vxe-table'
  import ALink from '/@/components/Link/Link.vue'
  import { FormEditType } from '/@/enums/formTypeEnum'
  import BasicImportModal from '/@/components/Bootx/BasicImport/BasicImportModal.vue'

  // 使用hooks
  const { handleTableChange, pageQueryResHandel, sortChange, resetQueryParams, pagination, pages, sortParam, model, loading } =
    useTablePage(queryPage)
  const { createMessage, createConfirm } = useMessage()

  // 查询条件
  const fields = computed(() => {
    return [
      { field: 'specialityName', type: STRING, name: '专业名称', placeholder: '请输入专业名称' },
    ] as QueryField[]
  })

  const xTable = $ref<VxeTableInstance>()
  const xToolbar = $ref<VxeToolbarInstance>()
  const majorEdit = $ref<any>()
  const basicImportModal = $ref<any>()

  onMounted(() => {
    vxeBind()
    queryPage()
  })
  function vxeBind() {
    xTable?.connect(xToolbar as VxeToolbarInstance)
  }

  /**
   * 分页查询
   */
  function queryPage() {
    loading.value = true
    // 查询列表
    page({
      ...model.queryParam,
      ...pages,
      ...sortParam,
    }).then(({ data }) => {
      pageQueryResHandel(data)
    })
    return Promise.resolve()
  }

  /**
   * 删除
   */
  function remove(record) {
    createConfirm({
      iconType: 'warning',
      title: '警告',
      content: `确定需要删除【${record.specialityName}】吗？`,
      onOk: () => {
        del(record.id).then(() => {
          createMessage.success('删除成功')
          queryPage()
        })
      },
    })
  }

  /**
   * 新增
   */
  function add() {
    majorEdit.init(null, FormEditType.Add)
  }

  // 编辑
  function edit(record) {
    majorEdit.init(record.id, FormEditType.Edit)
  }

  function importData() {
    basicImportModal.init()
  }
</script>

<style lang="less" scoped></style>
